package com.scujj.controller.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scujj.controller.UserHomePageController;
import com.scujj.pojo.Result;
import com.scujj.pojo.Subcribe;
import com.scujj.pojo.User;
import com.scujj.service.SubcribeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/home")
@Api(tags = "用户主页信息接口")
public class UserHomePageControllerImpl implements UserHomePageController {
    @Autowired
    private SubcribeService subcribeService;

    @Override
    @GetMapping("/subcribe")
    @ApiOperation("获取关注列表")
    public Result getSubcribeUserList(@RequestParam("id") Long userId) {
        if (userId == null) {
            return Result.error("参数错误");
        }
        User user = subcribeService.getUserWithSubscribeList(userId);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            return Result.success(user);
        }
    }

    @Override
    @GetMapping("/subcribe/count")
    @ApiOperation("获取关注数")
    public Result getCountSubcribe(@RequestParam("id") Long userId) {
        QueryWrapper<Subcribe> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", userId);
        long count = subcribeService.count(wrapper);
        return Result.success(count);
    }

    @Override
    @GetMapping("/fans")
    @ApiOperation("获取粉丝列表")
    public Result getFanList(@RequestParam("id") Long userId) {
        if (userId == null) {
            return Result.error("参数错误");
        }
        User user = subcribeService.getUserWithFansList(userId);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            return Result.success(user);
        }
    }

    @Override
    @GetMapping("/fans/count")
    @ApiOperation("获取粉丝数")
    public Result getCountFun(@RequestParam("id") Long userId) {
        QueryWrapper<Subcribe> wrapper = new QueryWrapper<>();
        wrapper.eq("sid", userId);
        long count = subcribeService.count(wrapper);
        return Result.success(count);
    }
}
